/**
 * created by 小卷毛, 2020/9/16
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.lib_tools_database.factory;

import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.bean.ControlProcess;
import com.sgcc.pda.lib_tools_database.bean.MeasureRelation;
import com.sgcc.pda.lib_tools_database.bean.MeasureRisk;
import com.sgcc.pda.lib_tools_database.bean.MeasureSummary;
import com.sgcc.pda.lib_tools_database.bean.RiskPointMeasure;
import com.sgcc.pda.lib_tools_database.bean.TypicalExp;
import com.sgcc.pda.lib_tools_database.bean.VideoStandard;
import com.sgcc.pda.lib_tools_database.bean.WorkContent;
import com.sgcc.pda.lib_tools_database.bean.other.AppNoForPlan;
import com.sgcc.pda.lib_tools_database.bean.other.ControlProcessDetail;
import com.sgcc.pda.lib_tools_database.bean.other.ProcessRiskData;
import com.sgcc.pda.lib_tools_database.bean.other.ProcessRiskDataItem;
import com.sgcc.pda.lib_tools_database.bean.other.RiskCheckState;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasure;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureProcess;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureResult;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureRisk;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureSave;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureStep;
import com.sgcc.pda.lib_tools_database.gen.ControlProcessDao;
import com.sgcc.pda.lib_tools_database.gen.MeasureRelationDao;
import com.sgcc.pda.lib_tools_database.gen.MeasureRiskDao;
import com.sgcc.pda.lib_tools_database.gen.MeasureSummaryDao;
import com.sgcc.pda.lib_tools_database.gen.RiskCheckStateDao;
import com.sgcc.pda.lib_tools_database.gen.RiskPointMeasureDao;
import com.sgcc.pda.lib_tools_database.gen.TicketMeasureDao;
import com.sgcc.pda.lib_tools_database.gen.TicketMeasureSaveDao;
import com.sgcc.pda.lib_tools_database.gen.TypicalExpDao;
import com.sgcc.pda.lib_tools_database.gen.VideoStandardDao;
import com.sgcc.pda.lib_tools_database.gen.WorkContentDao;

import org.greenrobot.greendao.Property;

import java.util.ArrayList;
import java.util.List;

/**
 * 描 述：JSBridge查询工具类
 * 作 者：小卷毛  2020-09-16 20:16
 * 修改描述：增加参数字段
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class KnowledgeCheck {

    /**
     * 获取知识库专业列表 第一级
     * 方法名称 loadKnowledgeMajorCls
     */
    public List<ControlProcess> getProfession() {
        return new ControlProcessImpl().findByParam(
                new Property[]{ControlProcessDao.Properties.CodeType},
                new String[]{"01"},
                ControlProcessDao.Properties.OrderNum,
                true
        );
    }

    /**
     * 获取知识库下级分类列表 第三/四级
     * 方法名称 loadKnowledgeMajorCls
     *
     * @param workNo 工作编号
     * @return 集合
     */
    public List<ControlProcess> getNextProfessionList(String workNo) {
        return new ControlProcessImpl().findByParam(
                new Property[]{ControlProcessDao.Properties.PNo},
                new String[]{workNo},
                ControlProcessDao.Properties.OrderNum,
                true
        );
    }

    /**
     * 获取知识库业务分类列表 第二级
     * 方法名称 loadKnowledgeBusiCls
     *
     * @param workNo 工作编号
     * @return 集合
     */
    public List<ControlProcess> getProfessionList(String workNo) {
        List<ControlProcess> list = getNextProfessionList(workNo);

        for (ControlProcess item : list) {
            // 业务项
            List<ControlProcess> innerList = getNextProfessionList(item.getWorkNo());
            item.setWorks(innerList);

            int markCount = new VideoStandardImpl().queryCount(
                    VideoStandardDao.Properties.WorkNo,
                    item.getWorkNo()
            );
            item.setStanVideoCount(markCount);

            int typicalCount = new TypicalExpImpl().queryCount(
                    TypicalExpDao.Properties.WorkNo,
                    item.getWorkNo()
            );
            item.setTypicalExpCount(typicalCount);
        }

        return list;
    }

    /**
     * 获取知识库工作程序（第三级）详情信息
     * 方法名称 loadKnowledgeProcDetail
     *
     * @param procNo 工作程序编号
     * @return 集合
     */
    public List<ControlProcessDetail> getThirdDetail(String procNo) {
        List<ControlProcessDetail> list = new ArrayList<>();
        // 查询工作步骤列表 一对多
        List<ControlProcess> stepList = getNextProfessionList(procNo);

        if (stepList != null) {
            for (ControlProcess item : stepList) {
                ControlProcessDetail data = new ControlProcessDetail();
                data.setRecordId(item.getRecordId());
                data.setTitle(item.getName());
                data.setContentDetail("");
                data.setMeasure("");

                // 查询工作内容 一对一
                WorkContent content = new WorkContentImpl()
                        .findData(WorkContentDao.Properties.WorkNo, item.getWorkNo());
                if (content != null) {
                    data.setContentDetail(content.getContentDetail());
                }

                // 查询防范措施 一对一
                MeasureRisk risk = new MeasureRiskImpl()
                        .findData(MeasureRiskDao.Properties.WorkNo, item.getWorkNo());
                if (risk != null) {
                    data.setMeasure(risk.getMeasure());
                }

                list.add(data);
            }
        }

        return list;
    }

    /**
     * 获取知识库标准视频列表（绑定到第二级）
     * 方法名称 loadKnowledgeVideo
     *
     * @param workNo 工作编号
     * @return 集合
     */
    public List<VideoStandard> getVideoList(String workNo) {
        return new VideoStandardImpl().findByParam(
                new Property[]{VideoStandardDao.Properties.WorkNo},
                new String[]{workNo}
        );
    }

    /**
     * 获取知识库典型经验列表（绑定到第二级）
     * 方法名称 loadKnowledgeTypicalExp
     *
     * @param workNo 工作编号
     * @return 集合
     */
    public List<TypicalExp> getExperienceList(String workNo) {
        return new TypicalExpImpl().findByParam(
                new Property[]{TypicalExpDao.Properties.WorkNo},
                new String[]{workNo}
        );
    }

    /**
     * 获取知识库典型经验详情
     *
     * @param recordId 记录Id
     * @return 对象
     */
    public TypicalExp getExperienceDetail(String recordId) {
        return new TypicalExpImpl().findData(TypicalExpDao.Properties.RecordId, recordId);
    }

    /**
     * 查询操作步骤及风险点
     * 方法名称 loadOperRiskPoint
     *
     * @param processNos 工作程序编号集合
     * @return 集合
     */
    public List<ProcessRiskData> getProcessRiskList(List<String> processNos) {
        List<ProcessRiskData> list = new ArrayList<>();

        if (processNos != null) {
            for (String processNo : processNos) {
                // 查询工作步骤 一对多
                List<ControlProcess> innerList = getNextProfessionList(processNo);

                if (innerList != null) {
                    for (ControlProcess inner : innerList) {
                        ProcessRiskData innerData = new ProcessRiskData();
                        innerData.setStepId(inner.getRecordId());
                        innerData.setName(inner.getName());
                        innerData.setStepNo(inner.getWorkNo());
                        innerData.setOrderNum(inner.getOrderNum());

                        // 查询防范措施与工作步骤关系 一对多
                        List<MeasureRelation> relationList =
                                new MeasureRelationImpl().findByParam(
                                        new Property[]{MeasureRelationDao.Properties.WorkNo},
                                        new String[]{inner.getWorkNo()},
                                        MeasureRelationDao.Properties.OrderNum,
                                        true
                                );

                        List<ProcessRiskDataItem> itemList = new ArrayList<>();

                        if (relationList != null) {
                            for (MeasureRelation item : relationList) {
                                // 查询防范措施 一对一
                                MeasureSummary summaryData =
                                        new MeasureSummaryImpl().findData(
                                                MeasureSummaryDao.Properties.MeaSumNo,
                                                item.getMeaSumNo()
                                        );

                                if (summaryData != null
                                        && "1".equals(summaryData.getIsLook())) {
                                    ProcessRiskDataItem risk = new ProcessRiskDataItem();
                                    risk.setMeaSumNo(summaryData.getMeaSumNo());
                                    risk.setMeaSumContent(summaryData.getMeaSumContent());
                                    risk.setHasRisk(summaryData.getHasRisk());
                                    risk.setRiskNo(summaryData.getRiskNo());

                                    risk.setIsValid("0");
                                    risk.setIsObtEvi("0");
                                    risk.setPicNumMin("");
                                    risk.setPicNumMax("");
                                    risk.setRiskPoint("");
                                    risk.setKeyMeasure("");

                                    if ("1".equals(summaryData.getHasRisk())) {
                                        // 查询风险点 一对一
                                        RiskPointMeasure point = new RiskPointMeasureImpl().findData(
                                                RiskPointMeasureDao.Properties.RiskNo,
                                                summaryData.getRiskNo()
                                        );

                                        if (point != null) {
                                            risk.setIsValid(point.getIsValid());
                                            risk.setIsObtEvi(point.getIsObtEvi());
                                            risk.setPicNumMin(point.getPicNumMin());
                                            risk.setPicNumMax(point.getPicNumMax());
                                            risk.setRiskPoint(point.getRiskPoint());
                                            risk.setKeyMeasure(point.getKeyMeasure());
                                        }
                                    }

                                    itemList.add(risk);
                                }
                            }
                        }

                        innerData.setMeasures(itemList);

                        list.add(innerData);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 查询关键风险点取证列表
     * 方法名称 loadRiskPointEvidence
     *
     * @param stepNos 工作步骤编号集合
     * @return 集合
     */
    public List<ProcessRiskData> getProcessRiskEvidenceList(List<String> stepNos) {
        List<ProcessRiskData> list = new ArrayList<>();

        if (stepNos != null) {
            for (String stepNo : stepNos) {
                // 查询工作步骤 一对一
                ControlProcess process = new ControlProcessImpl()
                        .queryData(ControlProcessDao.Properties.WorkNo, stepNo);

                if (process != null) {
                    // 查询防范措施与工作步骤关系 一对多
                    List<MeasureRelation> relationList =
                            new MeasureRelationImpl().findByParam(
                                    new Property[]{MeasureRelationDao.Properties.WorkNo},
                                    new String[]{stepNo},
                                    MeasureRelationDao.Properties.OrderNum,
                                    true
                            );

                    List<ProcessRiskDataItem> itemList = new ArrayList<>();

                    if (relationList != null) {
                        for (MeasureRelation item : relationList) {
                            // 查询防范措施 一对一
                            MeasureSummary summaryData =
                                    new MeasureSummaryImpl().findData(
                                            MeasureSummaryDao.Properties.MeaSumNo,
                                            item.getMeaSumNo()
                                    );

                            if (summaryData != null
                                    && "1".equals(summaryData.getIsLook())
                                    && "1".equals(summaryData.getHasRisk())) {
                                // 查询风险点 一对一
                                RiskPointMeasure point = new RiskPointMeasureImpl().findData(
                                        RiskPointMeasureDao.Properties.RiskNo,
                                        summaryData.getRiskNo()
                                );

                                if (point != null
                                        && "1".equals(point.getIsValid())
                                        && "1".equals(point.getIsObtEvi())) {
                                    ProcessRiskDataItem risk = new ProcessRiskDataItem();
                                    risk.setMeaSumNo(summaryData.getMeaSumNo());
                                    risk.setMeaSumContent(summaryData.getMeaSumContent());
                                    risk.setHasRisk(summaryData.getHasRisk());
                                    risk.setRiskNo(summaryData.getRiskNo());

                                    risk.setIsValid(point.getIsValid());
                                    risk.setIsObtEvi(point.getIsObtEvi());
                                    risk.setPicNumMin(point.getPicNumMin());
                                    risk.setPicNumMax(point.getPicNumMax());
                                    risk.setRiskPoint(point.getRiskPoint());
                                    risk.setKeyMeasure(point.getKeyMeasure());

                                    itemList.add(risk);
                                }
                            }
                        }
                    }

                    if (!itemList.isEmpty()) {
                        ProcessRiskData innerData = new ProcessRiskData();
                        innerData.setStepId(process.getRecordId());
                        innerData.setName(process.getName());
                        innerData.setStepNo(process.getWorkNo());
                        innerData.setOrderNum(process.getOrderNum());
                        innerData.setMeasures(itemList);

                        list.add(innerData);
                    }
                }
            }
        }

        return list;
    }

    /**
     * 查询关键风险点取证记录
     * 方法名称 loadRiskPointEvidenceRecord
     *
     * @param stepNos 工作步骤编号集合
     * @return 集合
     */
    public List<RiskCheckState> getRiskCheckStateList(String appNo, List<String> stepNos) {
        List<RiskCheckState> list = new ArrayList<>();

        if (stepNos != null) {
            for (String stepNo : stepNos) {
                List<RiskCheckState> innerList = new RiskCheckStateImpl().findByParam(
                        new Property[]{
                                RiskCheckStateDao.Properties.AppNo,
                                RiskCheckStateDao.Properties.StepNo,
                        },
                        new String[]{appNo, stepNo}
                );

                if (innerList != null
                        && innerList.size() > 0)
                    list.add(innerList.get(0));
            }
        }

        return list;
    }

    /**
     * 查询标准化作业步骤及风险点
     * 方法名称 loadOperStepRiskPoint
     *
     * @param appNo        工单编号
     * @param planNo       计划明细编号
     * @param isFilter     是否根据工作程序过滤
     * @param isShowAppend 是否显示自定义
     * @param processNos   工作程序编号
     * @return 对象
     */
    public TicketMeasureResult getTicketRiskList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo,
            String isFilter,
            String isShowAppend,
            List<String> processNos
    ) {
        List<TicketMeasureProcess> knowledge = new ArrayList<>();
        List<TicketMeasureRisk> custom = new ArrayList<>();
        int riskCount = 0;

        // 根据计划明细编号显查询工作票编号
        AppNoForPlan relationData =
                new AppNoForPlanImpl().getDataByPlanNo(planNo);

        // 根据工单编号显查询工作票编号
        if (relationData == null) {
            relationData = new AppNoForPlanImpl()
                    .getDataByAppNo(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo);
        }

        if (relationData != null) {
            // 查询当前工作票下的所有防范措施 一对多
            List<TicketMeasure> items = new TicketMeasureImpl()
                    .findByParam(
                            new Property[]{TicketMeasureDao.Properties.WtNo},
                            new String[]{relationData.getWtNo()},
                            TicketMeasureDao.Properties.WorkOrderNo
                    );

            if (items != null && items.size() > 0) {
                List<String> processList = new ArrayList<>();

                for (TicketMeasure measure : items) {
                    // 工作步骤为空表示是自定义防范措施
                    if ("1".equals(isShowAppend)
                            && TextUtils.isEmpty(measure.getWorkStepNo())) {
                        TicketMeasureRisk risk = new TicketMeasureRisk();
                        risk.setRecordId(measure.getRecordId());
                        risk.setMeasure(measure.getMeasure());
                        risk.setMeasureNo(measure.getMeasureNo());
                        risk.setRiskPicMin(measure.getRiskPicMin());
                        risk.setRiskPicMax(measure.getRiskPicMax());
                        risk.setRiskPointEvi("1");
                        custom.add(risk);
                        riskCount += 1;
                    } else {
                        if (!processList.contains(measure.getWorkNo())) {
                            processList.add(measure.getWorkNo());

                            TicketMeasureProcess process = new TicketMeasureProcess();
                            process.setDeviceTypeName(measure.getDeviceTypeName());
                            process.setWorkNo(measure.getWorkNo());
                            process.setWorkNoName(measure.getWorkNoName());
                            process.setWorkOrderNo(measure.getWorkOrderNo());

                            if ("1".equals(isFilter)) {
                                if (processNos.contains(measure.getWorkNo()))
                                    knowledge.add(process);
                            } else knowledge.add(process);
                        }
                    }
                }

                if (knowledge.size() > 0) {
                    for (TicketMeasureProcess process : knowledge) {
                        // 根据工作票和工作程序查询工作步骤 一对多
                        List<TicketMeasure> stepList = new TicketMeasureImpl()
                                .findByParam(
                                        new Property[]{
                                                TicketMeasureDao.Properties.WtNo,
                                                TicketMeasureDao.Properties.WorkNo,
                                        },
                                        new String[]{
                                                relationData.getWtNo(),
                                                process.getWorkNo()
                                        },
                                        TicketMeasureDao.Properties.WorkStepOrderNo
                                );

                        if (stepList != null) {
                            List<String> stepStrs = new ArrayList<>();
                            List<TicketMeasureStep> innerList = new ArrayList<>();

                            for (TicketMeasure inner : stepList) {
                                if (!stepStrs.contains(inner.getWorkStepNo())) {
                                    stepStrs.add(inner.getWorkStepNo());

                                    TicketMeasureStep step = new TicketMeasureStep();
                                    step.setWorkStepNo(inner.getWorkStepNo());
                                    step.setWorkStepName(inner.getWorkStepName());
                                    step.setWorkStepOrderNo(inner.getWorkStepOrderNo());
                                    innerList.add(step);
                                }
                            }

                            if (innerList.size() > 0) {
                                for (TicketMeasureStep step : innerList) {
                                    // 根据工作票、工作程序、工作步骤查询防范措施 一对多
                                    List<TicketMeasure> stepInnerList = new TicketMeasureImpl()
                                            .findByParam(
                                                    new Property[]{
                                                            TicketMeasureDao.Properties.WtNo,
                                                            TicketMeasureDao.Properties.WorkNo,
                                                            TicketMeasureDao.Properties.WorkStepNo,
                                                    },
                                                    new String[]{
                                                            relationData.getWtNo(),
                                                            process.getWorkNo(),
                                                            step.getWorkStepNo()
                                                    }
                                            );

                                    List<TicketMeasureRisk> stepRiskList = new ArrayList<>();

                                    if (stepInnerList != null) {
                                        for (TicketMeasure inner : stepInnerList) {
                                            TicketMeasureRisk risk = new TicketMeasureRisk();
                                            risk.setRecordId(inner.getRecordId());
                                            risk.setMeasure(inner.getMeasure());
                                            risk.setMeasureNo(inner.getMeasureNo());
                                            risk.setRiskPointEvi(inner.getRiskPointEvi());
                                            risk.setRiskPicMin(inner.getRiskPicMin());
                                            risk.setRiskPicMax(inner.getRiskPicMax());
                                            stepRiskList.add(risk);
                                            riskCount += 1;
                                        }
                                    }

                                    step.setMeasures(stepRiskList);
                                }
                            }

                            process.setWorkSteps(innerList);
                        }
                    }
                }
            }
        }

        TicketMeasureResult result = new TicketMeasureResult();
        result.setKnowledge(knowledge);
        result.setCustom(custom);
        result.setRiskCount(riskCount);
        return result;
    }

    /**
     * 查询标准化作业风险取证列表
     * 方法名称 loadRiskPointEvid
     *
     * @param appNo        工单编号
     * @param planNo       计划明细编号
     * @param isFilter     是否根据工作程序过滤
     * @param isShowAppend 是否显示自定义
     * @param stepNos      工作步骤编号
     * @return 集合
     */
    public TicketMeasureResult getTicketRiskEvidenceList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo,
            String isFilter,
            String isShowAppend,
            List<String> stepNos
    ) {
        List<TicketMeasureProcess> knowledge = new ArrayList<>();
        List<TicketMeasureRisk> custom = new ArrayList<>();
        int riskCount = 0;

        // 根据计划明细编号显查询工作票编号
        AppNoForPlan relationData = new AppNoForPlanImpl().getDataByPlanNo(planNo);

        // 根据工单编号显查询工作票编号
        if (relationData == null) {
            relationData = new AppNoForPlanImpl()
                    .getDataByAppNo(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo);
        }

        if (relationData != null) {
            // 查询当前工作票下的所有防范措施 一对多
            List<TicketMeasure> items = new TicketMeasureImpl()
                    .findByParam(
                            new Property[]{TicketMeasureDao.Properties.WtNo},
                            new String[]{relationData.getWtNo()},
                            TicketMeasureDao.Properties.WorkOrderNo
                    );

            if (items != null && items.size() > 0) {
                List<String> processList = new ArrayList<>();
                List<TicketMeasure> measureList = new ArrayList<>();

                for (TicketMeasure measure : items) {
                    // 工作步骤为空表示是自定义防范措施
                    if ("1".equals(isShowAppend)
                            && TextUtils.isEmpty(measure.getWorkStepNo())) {
                        TicketMeasureRisk risk = new TicketMeasureRisk();
                        risk.setRecordId(measure.getRecordId());
                        risk.setMeasure(measure.getMeasure());
                        risk.setMeasureNo(measure.getMeasureNo());
                        risk.setRiskPicMin(measure.getRiskPicMin());
                        risk.setRiskPicMax(measure.getRiskPicMax());
                        risk.setRiskPointEvi("1");
                        custom.add(risk);
                        riskCount += 1;
                    } else {
                        if (!processList.contains(measure.getWorkNo())) {
                            processList.add(measure.getWorkNo());
                            measureList.add(measure);
                        }
                    }
                }

                if (measureList.size() > 0) {
                    for (TicketMeasure process : measureList) {
                        // 根据工作票和工作程序查询工作步骤 一对多
                        List<TicketMeasure> stepList = new TicketMeasureImpl()
                                .findByParam(
                                        new Property[]{
                                                TicketMeasureDao.Properties.WtNo,
                                                TicketMeasureDao.Properties.WorkNo,
                                        },
                                        new String[]{
                                                relationData.getWtNo(),
                                                process.getWorkNo()
                                        },
                                        TicketMeasureDao.Properties.WorkStepOrderNo
                                );

                        if (stepList != null) {
                            List<String> stepStrs = new ArrayList<>();
                            List<TicketMeasureStep> innerList = new ArrayList<>();
                            List<TicketMeasure> measureStepList = new ArrayList<>();

                            for (TicketMeasure inner : stepList) {
                                if ("1".equals(isFilter)) {
                                    if (stepNos.contains(inner.getWorkStepNo())) {
                                        if (!stepStrs.contains(inner.getWorkStepNo())) {
                                            stepStrs.add(inner.getWorkStepNo());
                                            measureStepList.add(inner);
                                        }
                                    }
                                } else {
                                    if (!stepStrs.contains(inner.getWorkStepNo())) {
                                        stepStrs.add(inner.getWorkStepNo());
                                        measureStepList.add(inner);
                                    }
                                }
                            }

                            if (measureStepList.size() > 0) {
                                for (TicketMeasure step : measureStepList) {
                                    // 根据工作票、工作程序、工作步骤查询防范措施 一对多
                                    List<TicketMeasure> stepInnerList = new TicketMeasureImpl()
                                            .findByParam(
                                                    new Property[]{
                                                            TicketMeasureDao.Properties.WtNo,
                                                            TicketMeasureDao.Properties.WorkNo,
                                                            TicketMeasureDao.Properties.WorkStepNo,
                                                            TicketMeasureDao.Properties.RiskPointEvi
                                                    },
                                                    new String[]{
                                                            relationData.getWtNo(),
                                                            process.getWorkNo(),
                                                            step.getWorkStepNo(),
                                                            "1"
                                                    }
                                            );

                                    List<TicketMeasureRisk> stepRiskList = new ArrayList<>();

                                    if (stepInnerList != null) {
                                        for (TicketMeasure inner : stepInnerList) {
                                            TicketMeasureRisk risk = new TicketMeasureRisk();
                                            risk.setRecordId(inner.getRecordId());
                                            risk.setMeasure(inner.getMeasure());
                                            risk.setMeasureNo(inner.getMeasureNo());
                                            risk.setRiskPointEvi(inner.getRiskPointEvi());
                                            risk.setRiskPicMin(inner.getRiskPicMin());
                                            risk.setRiskPicMax(inner.getRiskPicMax());
                                            stepRiskList.add(risk);
                                            riskCount += 1;
                                        }
                                    }

                                    if (stepRiskList.size() > 0) {
                                        TicketMeasureStep stepData = new TicketMeasureStep();
                                        stepData.setWorkStepNo(step.getWorkStepNo());
                                        stepData.setWorkStepName(step.getWorkStepName());
                                        stepData.setWorkStepOrderNo(step.getWorkStepOrderNo());
                                        stepData.setMeasures(stepRiskList);
                                        innerList.add(stepData);
                                    }
                                }
                            }

                            if (innerList.size() > 0) {
                                TicketMeasureProcess processData = new TicketMeasureProcess();
                                processData.setDeviceTypeName(process.getDeviceTypeName());
                                processData.setWorkNo(process.getWorkNo());
                                processData.setWorkNoName(process.getWorkNoName());
                                processData.setWorkOrderNo(process.getWorkOrderNo());
                                processData.setWorkSteps(innerList);
                                knowledge.add(processData);
                            }
                        }
                    }
                }
            }
        }

        TicketMeasureResult result = new TicketMeasureResult();
        result.setKnowledge(knowledge);
        result.setCustom(custom);
        result.setRiskCount(riskCount);
        return result;
    }

    /**
     * 查询标准化作业关键风险点取证记录
     * 方法名称 loadRiskPointEvidRecord
     *
     * @param appNo  工单编号
     * @param planNo 计划明细编号
     * @return 集合
     */
    public List<TicketMeasureSave> getTicketRiskSaveList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo
    ) {
        AppNoForPlan relationData = new AppNoForPlanImpl().getDataByPlanNo(planNo);

        if (relationData == null) {
            relationData = new AppNoForPlanImpl()
                    .getDataByAppNo(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo);
        }

        if (relationData != null) {
            return new TicketMeasureSaveImpl().findByParam(
                    new Property[]{
                            TicketMeasureSaveDao.Properties.AppNo,
                            TicketMeasureSaveDao.Properties.PlanNo,
                    },
                    new String[]{relationData.getAppNo(), relationData.getPlanNo()});
        } else {
            return new TicketMeasureSaveImpl().findByParam(
                    new Property[]{
                            TicketMeasureSaveDao.Properties.AppNo,
                            TicketMeasureSaveDao.Properties.PlanNo,
                    },
                    new String[]{appNo, planNo});
        }
    }

    /**
     * 查询标准化作业防范措施列表
     * 方法名称
     *
     * @param appNo        工单编号
     * @param planNo       计划明细编号
     * @param isFilter     是否根据工作程序过滤
     * @param isShowAppend 是否显示自定义
     * @param stepNos      工作步骤编号
     * @return 集合
     */
    public List<TicketMeasureRisk> getTicketMeasureList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo,
            String isFilter,
            String isShowAppend,
            List<String> stepNos
    ) {
        List<TicketMeasureRisk> result = new ArrayList<>();
        List<TicketMeasureRisk> custom = new ArrayList<>();

        // 根据计划明细编号显查询工作票编号
        AppNoForPlan relationData = new AppNoForPlanImpl().getDataByPlanNo(planNo);

        // 根据工单编号显查询工作票编号
        if (relationData == null) {
            relationData = new AppNoForPlanImpl()
                    .getDataByAppNo(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo);
        }

        if (relationData != null) {
            // 查询当前工作票下的所有防范措施 一对多
            List<TicketMeasure> items = new TicketMeasureImpl()
                    .findByParam(
                            new Property[]{TicketMeasureDao.Properties.WtNo},
                            new String[]{relationData.getWtNo()},
                            TicketMeasureDao.Properties.WorkOrderNo
                    );

            if (items != null && items.size() > 0) {
                List<String> processList = new ArrayList<>();
                List<TicketMeasure> measureList = new ArrayList<>();

                for (TicketMeasure measure : items) {
                    // 工作步骤为空表示是自定义防范措施
                    if ("1".equals(isShowAppend)
                            && TextUtils.isEmpty(measure.getWorkStepNo())) {
                        TicketMeasureRisk risk = new TicketMeasureRisk();
                        risk.setRecordId(measure.getRecordId());
                        risk.setMeasure(measure.getMeasure());
                        risk.setMeasureNo(measure.getMeasureNo());
                        risk.setRiskPicMin(measure.getRiskPicMin());
                        risk.setRiskPicMax(measure.getRiskPicMax());
                        risk.setRiskPointEvi("1");
                        custom.add(risk);
                    } else {
                        if (!processList.contains(measure.getWorkNo())) {
                            processList.add(measure.getWorkNo());
                            measureList.add(measure);
                        }
                    }
                }

                if (measureList.size() > 0) {
                    for (TicketMeasure process : measureList) {
                        // 根据工作票和工作程序查询工作步骤 一对多
                        List<TicketMeasure> stepList = new TicketMeasureImpl()
                                .findByParam(
                                        new Property[]{
                                                TicketMeasureDao.Properties.WtNo,
                                                TicketMeasureDao.Properties.WorkNo,
                                        },
                                        new String[]{
                                                relationData.getWtNo(),
                                                process.getWorkNo()
                                        },
                                        TicketMeasureDao.Properties.WorkStepOrderNo
                                );

                        if (stepList != null) {
                            List<String> stepStrs = new ArrayList<>();
                            List<TicketMeasure> measureStepList = new ArrayList<>();

                            for (TicketMeasure inner : stepList) {
                                if ("1".equals(isFilter)) {
                                    if (stepNos.contains(inner.getWorkStepNo())) {
                                        if (!stepStrs.contains(inner.getWorkStepNo())) {
                                            stepStrs.add(inner.getWorkStepNo());
                                            measureStepList.add(inner);
                                        }
                                    }
                                } else {
                                    if (!stepStrs.contains(inner.getWorkStepNo())) {
                                        stepStrs.add(inner.getWorkStepNo());
                                        measureStepList.add(inner);
                                    }
                                }
                            }

                            if (measureStepList.size() > 0) {
                                for (TicketMeasure step : measureStepList) {
                                    // 根据工作票、工作程序、工作步骤查询防范措施 一对多
                                    List<TicketMeasure> stepInnerList = new TicketMeasureImpl()
                                            .findByParam(
                                                    new Property[]{
                                                            TicketMeasureDao.Properties.WtNo,
                                                            TicketMeasureDao.Properties.WorkNo,
                                                            TicketMeasureDao.Properties.WorkStepNo,
                                                            TicketMeasureDao.Properties.RiskPointEvi
                                                    },
                                                    new String[]{
                                                            relationData.getWtNo(),
                                                            process.getWorkNo(),
                                                            step.getWorkStepNo(),
                                                            "1"
                                                    }
                                            );

                                    if (stepInnerList != null) {
                                        for (TicketMeasure inner : stepInnerList) {
                                            TicketMeasureRisk risk = new TicketMeasureRisk();
                                            risk.setRecordId(inner.getRecordId());
                                            risk.setMeasure(inner.getMeasure());
                                            risk.setMeasureNo(inner.getMeasureNo());
                                            risk.setRiskPointEvi(inner.getRiskPointEvi());
                                            risk.setRiskPicMin(inner.getRiskPicMin());
                                            risk.setRiskPicMax(inner.getRiskPicMax());
                                            result.add(risk);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        result.addAll(custom);
        return result;
    }

}
